<?php

namespace App\Http\Controllers\Api;

use App\Events\SendNoticeToOne;
use App\Http\Requests\Api\WithdrawRequest;
use App\Models\Withdraw;
use Illuminate\Http\Request;

/**
 * @title 提现模块
 * @description 接口说明
 * @header name:accessToken require:1 default: desc:用户token
 *
 */

class WithdrawController extends ApiController
{
    /**
     * @title 申请提现
     * @description
     * @author wjx
     * @url /api/withdraw/add_withdraw
     * @method POST
     *
     * @param name:total_amount type:int require:1 default: other: desc:提现金额(元)
     * @param name:speed type:int require:1 default: other: desc:1快速5%2普通3%
     *
     *
     */
    public function add_withdraw(Request $request, WithdrawRequest $withdrawRequest)
    {
        $user = $request->user;
        if($user->wallet->computing_power<20){
            return $this->failed('算力不足');
        }
        if(!$user->real_name){
            return $this->failed('请先完成实名认证');
        }
        if(!$user->weixin_withdraw_openid){
            return $this->failed('请先绑定提现微信号');
        }
        if ($withdrawRequest->total_amount > $user->wallet->money) {
            return $this->failed('余额不足');
        }
        $withdraw = new Withdraw();
        $withdraw->user_id = $user->id;
        $withdraw->speed = $request->speed;
        $withdraw->channel = 1;
        $withdraw->partner_trade_no = set_order_no();
        $withdraw->money = $withdrawRequest->total_amount;        
        // $withdraw->money = 1;//一元提现        
        $withdraw->type = 1;
        $withdraw->status = 0;//待审核
        $res = $withdraw->save();

        if ($res) {
            return $this->message('申请成功，请耐心等待平台处理');
        } else {
            return $this->failed('未知错误');
        }
    }
    // /**
    //  * @title 店铺收益提现
    //  * @description
    //  * @author wjx
    //  * @url /api/withdraw/add_shop_withdraw
    //  * @method POST
    //  *
    //  * @param name:total_amount type:int require:1 default: other: desc:提现金额(元)
    //  * @param name:speed type:int require:1 default: other: desc:1快速5%2普通3%
    //  *
    //  */
    public function add_shop_withdraw(Request $request, WithdrawRequest $withdrawRequest)
    {
        $user = $request->user;
        if($user->wallet->computing_power<20){
            return $this->failed('算力不足20');
        }
        if(!$user->real_name){
            return $this->failed('请先完成实名认证');
        }
        if(!$user->weixin_withdraw_openid){
            return $this->failed('请先绑定提现微信号');
        }
        if ($withdrawRequest->total_amount > $user->wallet->shop_money) {
            return $this->failed('余额不足');
        }
        $withdraw = new Withdraw();
        $withdraw->user_id = $user->id;
        $withdraw->channel = 2;
        $withdraw->partner_trade_no = set_order_no();
        $withdraw->money = $withdrawRequest->total_amount;        
        $withdraw->type = 1;
        $withdraw->status = 0;//待审核
        $res = $withdraw->save();

        if ($res) {
            return $this->message('申请成功，请耐心等待平台处理');
        } else {
            return $this->failed('未知错误');
        }
    }
    /**
     * @title 退还押金页面
     * @description 接口说明
     * @author wjx
     * @url /api/withdraw/refund
     * @method POST
     * @return deposit:押金金额
     * @return weixin_withdraw_nickname:提现微信昵称
     */
    public function refund(Request $request)
    {
        $user = $request->user;
        $deposit = $user->wallet->deposit;

        if(!$user->weixin_withdraw_openid){
            return $this->failed('请先绑定微信提现账号');
        }else{
            $weixin_withdraw_nickname = $user->weixin_withdraw_nickname;
        }

        return $this->success(compact('deposit','weixin_withdraw_nickname'));
        
    }
    /**
     * @title 退还押金按钮
     * @description 接口说明
     * @author wjx
     * @url /api/withdraw/refund_confirm
     * @method POST
     */
    public function refund_confirm(Request $request)
    {
        $user = $request->user;
        $wallet = $user->wallet;
        if($wallet->deposit < 500){
            return $this->failed('押金余额不足');
        }
        $withdraw = new Withdraw();
        $withdraw->user_id = $user->id;
        $withdraw->channel = 3;//押金退还
        $withdraw->partner_trade_no = set_order_no();
        $withdraw->money = 500;
        $withdraw->type = 1;
        $withdraw->status = 0;//待审核
        $res = $withdraw->save();

        if($res){
            event(new SendNoticeToOne($withdraw,2,8));
            return $this->message('申请成功，请耐心等待后台审核');
        }else{
            return $this->failed('出现错误');
        }

    }
    /**
     * @title 我的提现记录(余额)
     * @description
     * @author wjx
     * @url /api/withdraw/my_withdraw_list
     * @method POST
     * 
     * @param name:loadType type:string require:1 default:more other: desc:方法参数:more/refresh
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     *
     */
    public function my_withdraw_list(Request $request,Withdraw $withdraw)
    {
        $user = $request->user;
        $startId = $request->ID ? $request->ID : 0;

        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }

        if ($loadType == 'more') {
            $lists = $withdraw->filters([['user_id','=',$user->id],['channel','=',1],['id', '<', $startId]])->take($offset)->orderBy('id','desc')->get();
        } else {
            $lists = $withdraw->filters([['user_id','=',$user->id],['channel','=',1],['id', '>', $startId]])->take($offset)->orderBy('id','desc')->get();
        }

        $lists->transform(function($list){
            switch($list->status){
                case '0':
                    $status = '待审核';
                    break;
                case '1':
                    $status = '已通过';                    
                    break;                       
                case '2':
                    $status = '已取消';                    
                    break;                       
                case '3':
                    $status = '未通过';                    
                    break;                       
                default:
                    break;
            }
            
            $type = '余额提现';           
            
            return [
                'id' => $list->id,
                'type' => $type,
                'created_at' => $list->created_at->toDateTimeString(),
                'money' => $list->money,
                'status' => $status,
            ];
        });
        if($lists){
            return $this->success($lists);
        }else{
            $lists = [];
            return $this->success($lists);
        }
    }
    /**
     * @title 我的提现记录(收益)
     * @description
     * @author wjx
     * @url /api/withdraw/my_withdraw_list_shop
     * @method POST
     * @param name:loadType type:string require:1 default:more other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:startId type:int require:1 default:0 other: desc:数据起点ID
     * @param name:historyId type:int require:1 default:0 other: desc:刷新接口ID
     *
     */
    public function my_withdraw_list_shop(Request $request,Withdraw $withdraw)
    {
        $user = $request->user;
        $startId = $request->startId ? $request->startId : 0;
        $historyId = $request->historyId ? $request->historyId : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->loadType;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('loadType参数错误');
        }

        if ($loadType == 'more') {
            $lists = $withdraw->filters([['user_id','=',$user->id],['channel','=',2],['id', '<', $startId]])->take($offset)->orderBy('id','desc')->get();
        } else {
            $lists = $withdraw->filters([['user_id','=',$user->id],['channel','=',2],['id', '>', $historyId]])->take($offset)->orderBy('id','desc')->get();
        }
        $lists->transform(function($list){
            switch($list->status){
                case '0':
                    $status = '待审核';
                    break;
                case '1':
                    $status = '已通过';                    
                    break;                       
                case '2':
                    $status = '已取消';                    
                    break;                       
                case '3':
                    $status = '未通过';                    
                    break;                       
                default:
                    break;
            }

            $type = '收益提现';

            return [
                'type' => $type,
                'created_at' => $list->created_at->toDateTimeString(),
                'money' => $list->money,
                'status' => $status,
            ];
        });
        if($lists){
            return $this->success($lists);
        }else{
            $lists = [];
            return $this->success($lists);
        }
    }

}
